packageentity;importstaticorg.junit.Assert.assertEquals;importjava.util.List;importjavax.persistence.EntityManager;importorg.junit.Test;publicclass CompanyTest extends TestBase {private Company findCompanyNamed(final EntityManager em, String name){return(Company) em.createQuery("select c from Company c where c.name=?1")
.setParameter(1, name).getSingleResult();}
@Test
publicvoid createCompany(){final Company c1 = new Company();
c1.setName("The Company");
c1.setAddress(new Address("D Rd.", "", "Paris", "TX", "77382"));
em.getTransaction().begin();
em.persist(c1);
em.getTransaction().commit();final Company foundCompany = findCompanyNamed(em, "The Company");
assertEquals("D Rd.", foundCompany.getAddress().getStreetAddress1());// Note, we do not need an assert. Why? the method getSingleResult()// will throw an exception if there is not exactly one// object found. We'll research that in the second JPA tutorial.}private Company createCompanyWithTwoEmployees(){final Company c1 = new Company();
c1.setName("The Company");
c1.setAddress(new Address("D Rd.", "", "Paris", "TX", "77382"));finalList<Person> people = PersonTest.generatePersonObjects();for(Person p : people){
c1.hire(p);}
em.getTransaction().begin();for(Person p : people){
em.persist(p);}
em.persist(c1);
em.getTransaction().commit();return c1;}
@SuppressWarnings("unchecked")
@Test
publicvoid createCompanyAndHirePeople(){
createCompanyWithTwoEmployees();finalList<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());final Company foundCompany = (Company) em.createQuery("select c from Company c where c.name=?1").setParameter(1,
"The Company").getSingleResult();
assertEquals(2, foundCompany.getEmployees().size());}
@Test
publicvoid hireAndFire(){final Company c1 = createCompanyWithTwoEmployees();finalList<Person> people = PersonTest.generatePersonObjects();
em.getTransaction().begin();for(Person p : people){
c1.fire(p);}
em.persist(c1);
em.getTransaction().commit();final Company foundCompany = findCompanyNamed(em, "The Company");
assertEquals(0, foundCompany.getEmployees().size());}}
PersonTest.java
packageentity;importstaticorg.junit.Assert.assertEquals;importstaticorg.junit.Assert.assertTrue;importjava.util.ArrayList;importjava.util.List;importorg.junit.Test;publicclass PersonTest extends TestBase {publicstaticList<Person> generatePersonObjects(){finalList<Person> people = newArrayList<Person>();final Address a1 = new Address("A Rd.", "", "Dallas", "TX", "75001");final Person p1 = new Person("Brett", 'L', "Schuchert", a1);final Address a2 = new Address("B Rd.", "S2", "OkC", "OK", "73116");final Person p2 = new Person("FirstName", 'K', "LastName", a2);
people.add(p1);
people.add(p2);return people;}
@SuppressWarnings("unchecked")
@Test
publicvoid insertAndRetrieve(){finalList<Person> people = generatePersonObjects();
em.getTransaction().begin();for(Person p : people){
em.persist(p);}
em.getTransaction().commit();finalList<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());for(Person current : list){finalString firstName = current.getFirstName();finalString streetAddress1 = current.getAddress()
.getStreetAddress1();
assertTrue(firstName.equals("Brett")
|| firstName.equals("FirstName"));
assertTrue(streetAddress1.equals("A Rd.")
|| streetAddress1.equals("B Rd."));}}}
Person.java
Company.java
TestBase.java
CompanyTest.java
PersonTest.java